package com.aabte.feignhttp.springmvcdemo.common.remote;

import com.aabte.feignhttp.springmvcdemo.common.common.ResponseBean;
import com.aabte.feignhttp.springmvcdemo.common.common.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Daniel
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("/hello")
    public ResponseBean hello() {
        return testService.hello();
    }

    @GetMapping("/user")
    public ResponseBean user() {
        List<User> data = new ArrayList<>();
        data.add(new User(1, "tom"));
        data.add(new User(2, "jack"));
        return ResponseBean.success(data);
    }

    @GetMapping("/user/{name}")
    public ResponseBean userByName(
            @PathVariable("name") String name
    ) {
        return ResponseBean.success(new User(1, name));
    }

    @GetMapping("/user/url")
    public ResponseBean userByName2(
            @RequestParam String name
    ) {
        return ResponseBean.success(new User(1, name));
    }

    @PostMapping("/user")
    public ResponseBean addUser(
            @RequestBody User user
    ) {
        user.setId(3);
        user.setUsername(user.getUsername() + "_add");
        return ResponseBean.success(user);
    }

    @PutMapping("/user")
    public ResponseBean updateUser(
            @RequestBody User user
    ) {
        user.setUsername("tone_update");
        return ResponseBean.success(user);
    }

    @DeleteMapping("/user/{id}")
    public ResponseBean deleteUser(
            @PathVariable Integer id
    ) {
        return ResponseBean.success(new User(id, "delete"));
    }


}